<template>
  <div class="user_right fl ml20" v-loading="isloginaf">
    <div class="shipping_content">
      <table>
        <colgroup>
          <col width="112" />
          <col width="168" />
          <col width="90" />
          <col width="136" />
          <col width="120" />
          <col width="120" />
          <col width="86" />
          <col width="80" />
        </colgroup>
        <tbody>
          <tr class="tr_body_top">
            <th>申请品牌</th>
            <th>申请商铺名称</th>
            <th>负责人</th>
            <th>负责人电话</th>
            <th>申请日期</th>
            <th>审核日期</th>
            <th>状态</th>
            <th>操作</th>
          </tr>
        </tbody>
        <tbody>
          <tr class="tr_body_content" v-for="(item,index) in shoreList" :key="item.id">
            <td>
              <span style="margin:1px" v-for="(itema,indexa) in item.brands" :key="indexa">{{itema.name}}</span>
            </td>
            <td>{{item.name}}</td>
            <td>{{item.user_name}}</td>
            <td>{{item.phone}}</td>
            <td>{{item.created_at}}</td>
            <td>{{item.updated_at}}</td>
            <td>已审核</td>
            <td>
              <el-link type="danger" @click="delete_apdus(item.id,index)">删除</el-link>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="btn_application" @click="btnApplication">申请商铺</div>
    <el-dialog title="申请店铺" :visible.sync="shopsPopup" width="30%" center>
      <div>
        <el-form
          :model="storeData"
          status-icon
          :rules="rules"
          ref="storeData"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="商铺名称" prop="name">
            <el-input v-model="storeData.name"></el-input>
          </el-form-item>
          <el-form-item label="商铺负责人" prop="user_name">
            <el-input v-model="storeData.user_name"></el-input>
          </el-form-item>
          <el-form-item label="负责人电话" prop="phone">
            <el-input v-model.number="storeData.phone"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="shopsPopup = false">取 消</el-button>
        <el-button type="primary" @click="ascertain('storeData')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { ovistoreList, ovistore, deleteStore } from "@/api/business";
export default {
  data() {
    var checkMobile = (rule, value, callback) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
      if (regMobile.test(value)) {
        return callback();
      }
      callback(new Error("请输入合法的手机号"));
    };
    return {
      shopsPopup: false,
      isloginaf:true,
      shoreList: [],
      storeData: {
        name: "",
        user_name: "",
        phone: ""
      },
      rules: {
        name: [{ required: true, message: "请填写店铺名称", trigger: "blur" }],
        user_name: [
          { required: true, message: "请填写负责人名称", trigger: "blur" }
        ],
        phone: [{ required: true, validator: checkMobile, trigger: "blur" }]
      }
    };
  },
  created() {
    this.getOvistoreList();
  },
  methods: {
    async delete_apdus(id, index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.shoreList.splice(index, 1);
          deleteStore(id);
          this.$message({
            type: "success",
            message: "删除成功"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    // 获取店铺列表
    async getOvistoreList() {
      const res = await ovistoreList();
      this.shoreList = res;
      console.log(res);
      this.isloginaf = false;
    },

    btnApplication() {
      this.shopsPopup = true;
    },

    // 确定
    async ascertain(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          ovistore(this.storeData).then(
            e => (this.shopsPopup = false),
            this.getOvistoreList()
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.user_right {
  padding-bottom: 50px;
}
.shipping_content {
  display: flex;
  justify-content: center;
  margin-top: 50px;
}
.tr_body_top {
  height: 50px;
  width: 912px;
  background: #eeeeee;
}
.tr_body_content {
  height: 50px;
  border-bottom: 2px dashed #eeeeee;
  > td {
    text-align: center;
  }
}
.btn_application {
  width: 171px;
  height: 40px;
  background: #4448f8;
  border-radius: 5px;
  margin: 0 auto;
  line-height: 40px;
  font-size: 18px;
  color: #fff;
  text-align: center;
  margin-top: 50px;
  cursor: pointer;
  user-select: none;
  transition: all 0.2s ease-in-out;
  &:hover {
    background: #3e41bec7;
    transform: scale(0.9, 0.9);
  }
}
</style>